package com.szp.lg.blog.datasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataSourceContextHolder {
	public static final Logger log = LoggerFactory.getLogger(DataSourceContextHolder.class);
	
	/**
	 * 默认数据源
	 */
	public static final String DEFAULT_DATASOURCE_TYPE = DataSourceConfig.DEFAULT_DATASOURCE_TYPE;
	
	private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
	
	//设置数据源
	public static void setDataSourceType(String dataSourceType) {
		log.info("切换到{}数据源",dataSourceType);
		contextHolder.set(dataSourceType);
	}
	
	//获取数据源
	public static String getDataSourceType() {
		if(contextHolder.get() == null) {
			return DEFAULT_DATASOURCE_TYPE;
		}
		
		return contextHolder.get();
	}
	
	//清除数据源
	public static void clearDataSourceType() {
		contextHolder.remove();
	}
}
